﻿/*
 *  Copyright (C) 2010 by Benjamin Nolmans <benjamin.nolmans@gmail.com>
 *  and Barbara Post <postb99@gmail.com>
 *  Copyright (C) 2010 by Barbara Post postb99@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using System.Collections.Generic;
using OsamesFrameworkCore.Logging;

namespace OsamesFrameworkCore.Xml.Types
{
    public class XmlDictionaryContainer
    {
        #region attributes

        private readonly Dictionary<string, XmlDictionarySection> _xmlDictionary = new Dictionary<string, XmlDictionarySection>();

        #endregion

        /// <summary>
        /// Safe addition of data.
        /// </summary>
        /// <param name="strSectionName_"></param>
        /// <param name="xmlNodeName_"></param>
        /// <param name="xmlNodeValue_"></param>
        public void Add(string strSectionName_, string xmlNodeName_, string xmlNodeValue_)
        {
            XmlDictionarySection dicSection;

            if (!_xmlDictionary.ContainsKey(strSectionName_))
            {
                dicSection = new XmlDictionarySection();
                _xmlDictionary.Add(strSectionName_, dicSection);

            }
        /*    else
            {
                LoggerService.LogToOutput("[INFO]: [" + strSectionName_ + "] section already exists");
            } */

            dicSection = _xmlDictionary[strSectionName_];

            dicSection.Add(xmlNodeName_, xmlNodeValue_);
        }

        /// <summary>
        /// Find value with exception on not found section/node.
        /// </summary>
        /// <param name="strSection_"></param>
        /// <param name="strNodeName_"></param>
        /// <returns></returns>
        public string FindValue(string strSection_, string strNodeName_)
        {
            if (!_xmlDictionary.ContainsKey(strSection_))
            {
                XmlDictionaryException err = new XmlDictionaryException(string.Format(XmlDictionaryException.CST_STR_ERROR_SECTION_0_NOT_FOUND, strSection_));
                //LoggerService.LogToOutput(err.Message);
                throw err;
            }
            try
            {
                return _xmlDictionary[strSection_].FindValue(strNodeName_);
            }
            catch (XmlDictionaryException err)
            {
                throw new XmlDictionaryException(string.Format(XmlDictionaryException.CST_STR_ERROR_NODE_0_NOT_FOUND_IN_SECTION_1, strNodeName_, strSection_), err);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strSection_"></param>
        /// <param name="strNodeName_"></param>
        /// <param name="strValue_"></param>
        public void SetValue(string strSection_, string strNodeName_, string strValue_)
        {
            _xmlDictionary[strSection_].SetValue(strNodeName_, strValue_);

        }
    }
}
